package com.animal.service.impl;

import com.animal.entity.AnAnimal;
import com.animal.entity.AnApply;
import com.animal.entity.AnMeans;
import com.animal.entity.AnUser;
import com.animal.mapper.AnAnimalMapper;
import com.animal.mapper.AnApplyMapper;
import com.animal.mapper.AnMeansMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.service.AnApplyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author animal
 * @since 2022-02-07
 */
@Service
public class AnApplyServiceImpl extends ServiceImpl<AnApplyMapper, AnApply> implements AnApplyService {
  @Autowired
    AnApplyMapper anApplyMapper;
  @Autowired
    AnUserMapper anUserMapper;
  @Autowired
  AnAnimalMapper anAnimalMapper;
  @Autowired
    AnMeansMapper anMeansMapper;
    @Override
    public List<AnApply> findAll(int size, int current) {
        Page<AnApply> userPages = new Page<>(size,current);
        Page<AnApply> userPagess=  anApplyMapper.selectPage(userPages,null);
        List<AnApply> list=userPagess.getRecords();

        for(AnApply a:list){
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("user_id",a.getUserId());
            AnMeans anMeans =anMeansMapper.selectOne(wrapper);
            AnUser anUser=anUserMapper.selectById(a.getUserId());
            anUser.setAnMeans(anMeans);
            a.setAnUser(anUser);
            a.setAnimal(anAnimalMapper.selectById(a.getAnId()));
        }
        return list;
    }
    public List<AnApply> findByUserId(int size, int current,int userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        Page<AnApply> userPages = new Page<>(size,current);
        Page<AnApply> i=anApplyMapper.selectPage(userPages,queryWrapper);
        List<AnApply> list=i.getRecords();
        for(AnApply a:list){
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("user_id",a.getUserId());
            AnMeans anMeans =anMeansMapper.selectOne(wrapper);
            AnUser anUser=anUserMapper.selectById(a.getId());
            anUser.setAnMeans(anMeans);
            a.setAnUser(anUser);
            a.setAnimal(anAnimalMapper.selectById(a.getAnId()));
        }
        return list;
    }
}
